package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.entityoperatetype;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EntityOperateTypeMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Employee;
import com.cqhilink.iot.jobstandardmanagement.facade.model.EntityOperateType;
import com.cqhilink.iot.jobstandardmanagement.facade.service.entityoperatetype.JsmEntityOperateTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian@cqhilink.com on 2016/11/29.
 * 实体操作类型服务实现类
 */

public class JsmEntityOperateTypeServiceImpl implements JsmEntityOperateTypeService {
    @Autowired
    private EntityOperateTypeMapper entityOperateTypeMapper;


    /**
     * 操作生产实体类型查询数据问题
     * @param entityOperateType
     * @return
     */
    @Override
    public JSONObject toEntityOperateTypeTable(EntityOperateType entityOperateType) {
        Map map = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        jsonObject.put("total", entityOperateTypeMapper.queryCount(entityOperateType));
        List<EntityOperateType> entityOperateTypeList = entityOperateTypeMapper.queryList(entityOperateType);
        jsonObject.put("rows", JSON.toJSON(entityOperateTypeList));
        return jsonObject;
    }

    /**
     * 实体操作类型-新增
     * @param entityOperateType 实体操作类型
     * @return
     * @throws Exception
     */
    @Override
    public int addEntityOperate(EntityOperateType entityOperateType, Employee employee) throws Exception {
        String creatorId = employee.getEmployeeId();
        entityOperateType.setEntityOperateId(UniqueUtil.uuid());
        entityOperateType.setModifierId(creatorId);
        entityOperateType.setCreatorId(creatorId);
        entityOperateType.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        entityOperateType.setCreateEtime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        int i = entityOperateTypeMapper.insertSelective(entityOperateType);
        return i;
    }

    /**
     * 实体操作类型修改
     * @param entityOperateType entityOperateType
     * @return
     * @throws Exception
     */
    @Override
    public int updateEntityOperate(EntityOperateType entityOperateType, Employee employee) throws Exception {
        entityOperateType.setModifierId(employee.getEmployeeId());
        entityOperateType.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        int i = entityOperateTypeMapper.updateByPrimaryKeySelective(entityOperateType);
        return i;
    }

    /**
     * 实体操作类型-查询
     *
     * @param entityOperateId 根据实体操作类型唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public EntityOperateType entityTypefindById(String entityOperateId) throws Exception {
        return entityOperateTypeMapper.selectByPrimaryKey(entityOperateId);
    }

    /**
     * 删除实体操作类型-批量
     *
     * @param entityOperateIds 实体操作类型唯一标识查询条件
     * @return
     */
    @Override
    public int entityOperateTypeDelByIds(Map entityOperateIds) {
        return entityOperateTypeMapper.entityOperateTypeUpdateByIds(entityOperateIds);
    }

    /**
     * 查询实体操作类型列表
     *
     * @param query 实体操作类型唯一标识查询条件
     * @return
     * @throws Exception
     */
    @Override
    public List<EntityOperateType> queryEntityOperateTypeList(Map query) throws Exception {

        return entityOperateTypeMapper.queryEntityOperateTypeList(query);
    }

    /**
     * @param entityOperateType 操作类型组合数据
     * @throws Exception
     */
    @Override
    public EntityOperateType queryEntityOperateType(EntityOperateType entityOperateType) throws Exception {
        return entityOperateTypeMapper.selectByEntityOperateType(entityOperateType);
    }
}
